package com.group.sliding.controller;

import com.group.sliding.domain.dto.ItemFormDTO;
import com.group.sliding.domain.query.ItemPageQuery;
import com.group.sliding.domain.vo.ItemVO;
import com.group.sliding.enums.ItemStatus;
import com.group.sliding.result.PageDTO;
import com.group.sliding.result.Result;
import com.group.sliding.service.IItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "管理员商品管理相关接口")
@RestController
@RequestMapping("/admin/items")
@RequiredArgsConstructor
public class AdminItemController {
    private final IItemService itemService;

    @ApiOperation("新增商品")
    @PostMapping("/add")
    public Result<String> saveItem(ItemFormDTO itemFormDTO) {
        log.info("新增商品 {}", itemFormDTO);
        itemService.saveItem(itemFormDTO);
        log.info("新增商品成功");
        return Result.success();
    }

    @ApiOperation("更新商品状态")
    @PutMapping("/status/{id}/{status}")
    public Result<String> updateItemStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        log.info("更新商品状态 商品id: {}, status: {}", id, status);
        ItemStatus itemStatus = ItemStatus.of(status);
        itemService.updateItemStatus(id, itemStatus);
        log.info("更新商品状态成功 商品id: {}, {}", id, itemStatus);
        return Result.success();
    }

    @ApiOperation("更新商品")
    @PutMapping("/update")
    public Result<String> updateItem(ItemFormDTO itemFormDTO) {
        log.info("更新商品信息 {}", itemFormDTO);
        itemService.updateItem(itemFormDTO);
        log.info("更新商品信息成功 {}", itemFormDTO);
        return Result.success();
    }

    @ApiOperation("分页查询商品")
    @GetMapping("/page")
    public Result<PageDTO<ItemVO>> queryItemByPage(ItemPageQuery query) {
        log.info("分页查询商品 {}", query);
        PageDTO<ItemVO> result = itemService.search(query);
        log.info("分页查询商品结果 {}", result);
        return Result.success(result);
    }
}
